package com.elaine.controller;

import com.elaine.service.WxCallBackService;
import com.elaine.utils.CheckUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wrb
 * @date 2020/3/31 9:55
 */
@Slf4j
@Api(value = "微信回调入口")
@RestController
@CrossOrigin(origins = "*", methods = {RequestMethod.POST, RequestMethod.GET})
@RequestMapping("/wx/pb")
public class WxCallBackController {

	@Autowired
	private WxCallBackService wxCallBackService;

	@RequestMapping(value = "/callback", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
	@ApiOperation(value = "接收微信回调")
	public String callback(
			HttpServletRequest request,
			@RequestParam(value = "signature", required = false) String signature,
			@RequestParam String timestamp,
			@RequestParam String nonce,
			@RequestParam String openid
	) {
		log.info("----------------接收微信回调------------------");
		return wxCallBackService.processRequest(request);
	}

	@RequestMapping(value = "/callback", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
	@ApiOperation(value = "服务器配置检查")
	public String check(
			@RequestParam(value = "signature", required = false) String signature, @RequestParam String timestamp,
			@RequestParam String nonce, @RequestParam String echostr
	) {
		log.info("----------------服务器配置检查------------------");
		if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
			System.out.println(echostr);
		}
		return echostr;
	}
@RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
	@ApiOperation(value = "测试")
	public String test(
	) {
		log.info("----------------测试------------------");
		return "success";
	}

}
